package controller

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntityManager"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedisStatus"
	"gitee.com/fierce_wolf/go-fox-edge-common/commSql"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/OS"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEnv"
	"gitee.com/fierce_wolf/go-fox-edge-common/httpserver"
	"go-fox-edge-kernel-manager/utils/PageUtils"
)

type dashboardManageController struct {
}

func (e *dashboardManageController) init() {
	httpserver.Server.POST("/dashboard/indicator", e.queryIndicator)
}

func (e *dashboardManageController) queryIndicator(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	indicator := make(map[string]interface{})

	e.getChannelEntityIndicator(indicator)
	e.getObjectEntityIndicator(indicator)
	e.getDeviceStatusEntityIndicator(indicator)
	e.getDeviceEntityIndicator(indicator)
	e.getServiceEntityIndicator(indicator)
	e.getEdgeIndicator(indicator)

	return indicator, nil
}

func (e *dashboardManageController) getChannelEntityIndicator(indicators map[string]interface{}) {
	channelTotal := 0
	channelType := make(map[string]interface{})
	commEntityManager.GetEntityCount(edgeEntity.Type.ChannelEntity.Type(), func(entity commEntity.IEntity) bool {
		channelEntity := entity.(*edgeEntity.ChannelEntity)
		channelTotal++
		channelType[channelEntity.ChannelType] = nil
		return true
	})

	indicators["channelTotalCount"] = channelTotal
	indicators["channelTypeCount"] = len(channelType)

}

func (e *dashboardManageController) getObjectEntityIndicator(indicators map[string]interface{}) {
	// 查询总数
	selectCount := PageUtils.MakeSelectCountSQL("tb_device_object", "")
	total, err := commSql.QueryCount(selectCount)
	if err != nil {
		return
	}

	indicators["objectTotalCount"] = total
}

func (e *dashboardManageController) getDeviceStatusEntityIndicator(indicators map[string]interface{}) {
	successCount := 0
	failedCount := 0

	commEntityManager.GetEntityCount(edgeEntity.Type.DeviceStatusEntity.Type(), func(entity commEntity.IEntity) bool {
		deviceStatusEntity := entity.(*edgeEntity.DeviceStatusEntity)

		if deviceStatusEntity.CommFailedCount > 0 {
			failedCount++
		}
		if deviceStatusEntity.CommFailedCount == 0 {
			successCount++
		}

		return true
	})

	indicators["deviceFailedCount"] = failedCount
	indicators["deviceSuccessCount"] = successCount
}

func (e *dashboardManageController) getDeviceEntityIndicator(indicators map[string]interface{}) {
	deviceTotal := 0
	deviceType := make(map[string]interface{})
	commEntityManager.GetEntityCount(edgeEntity.Type.DeviceEntity.Type(), func(entity commEntity.IEntity) bool {
		deviceEntity := entity.(*edgeEntity.DeviceEntity)

		deviceTotal++
		deviceType[deviceEntity.DeviceType] = nil

		return true
	})

	indicators["deviceTotalCount"] = deviceTotal
	indicators["deviceTypeCount"] = len(deviceType)
}

func (e *dashboardManageController) getServiceEntityIndicator(indicators map[string]interface{}) {
	resultList := commRedisStatus.GetDataList(60 * 1000)

	indicators["serviceActiveCount"] = len(resultList)
}

func (e *dashboardManageController) getEdgeIndicator(indicators map[string]interface{}) {
	diskInfo := OS.Sys.ReadDiskInfo()
	memInfo := OS.Sys.ReadMemInfo()
	cpuInfo := OS.Sys.ReadCpuInfo()
	indicators["cpuUID"] = edgeEnv.GetCPUID()
	indicators["envType"] = edgeEnv.GetEnvType()
	indicators["arch"] = edgeEnv.GetARCH()

	indicators["diskSizeTxt"] = diskInfo["sizeTxt"]
	indicators["diskSize"] = Map.GetOrDefault(diskInfo, "size", 1)
	indicators["diskUsed"] = Map.GetOrDefault(diskInfo, "used", 1)
	indicators["diskAvail"] = Map.GetOrDefault(diskInfo, "avail", 1)
	indicators["diskUsePercentage"] = Map.GetOrDefault(diskInfo, "usePercentage", 1)

	indicators["ramTotalTxt"] = Map.GetOrDefault(memInfo, "ramTotalTxt", "")
	indicators["ramTotal"] = Map.GetOrDefault(memInfo, "ramTotal", 1)
	indicators["ramUsed"] = Map.GetOrDefault(memInfo, "ramUsed", 1)
	indicators["ramFree"] = Map.GetOrDefault(memInfo, "ramFree", 1)

	indicators["swapTotalTxt"] = Map.GetOrDefault(memInfo, "swapTotalTxt", "")
	indicators["swapTotal"] = Map.GetOrDefault(memInfo, "swapTotal", 1)
	indicators["swapUsed"] = Map.GetOrDefault(memInfo, "swapUsed", 0)
	indicators["swapFree"] = Map.GetOrDefault(memInfo, "swapFree", 1)

	indicators["cpuUs"] = Map.GetOrDefault(cpuInfo, "us", 1)
	indicators["cpuSy"] = Map.GetOrDefault(cpuInfo, "sy", 1)
	indicators["cpuNi"] = Map.GetOrDefault(cpuInfo, "ni", 1)
	indicators["cpuId"] = Map.GetOrDefault(cpuInfo, "id", 1)
	indicators["cpuWa"] = Map.GetOrDefault(cpuInfo, "wa", 1)
	indicators["cpuHi"] = Map.GetOrDefault(cpuInfo, "hi", 1)
	indicators["cpuSi"] = Map.GetOrDefault(cpuInfo, "si", 1)
	indicators["cpuSt"] = Map.GetOrDefault(cpuInfo, "st", 1)
}
